# Copyright (C) 2024 - 2025 Advanced Micro Devices, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT

cmake_minimum_required(VERSION 3.15)

include_directories(${CMAKE_BINARY_DIR}/include)
include(${CMAKE_CURRENT_SOURCE_DIR}/UserConfig.cmake)
set(APP_NAME asufw)
project(${APP_NAME})

find_package(common)
enable_language(C ASM)

collector_create (PROJECT_LIB_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}")
collector_create (PROJECT_LIB_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}")

collect (PROJECT_LIB_SOURCES xasufw_cmd.c)
collect (PROJECT_LIB_HEADERS xasufw_cmd.h)
collect (PROJECT_LIB_HEADERS xasufw_config.h)
collect (PROJECT_LIB_HEADERS xasufw_debug.h)
collect (PROJECT_LIB_SOURCES xasufw_dma.c)
collect (PROJECT_LIB_HEADERS xasufw_dma.h)
collect (PROJECT_LIB_HEADERS xasufw_hw.h)
collect (PROJECT_LIB_SOURCES xasufw_init.c)
collect (PROJECT_LIB_HEADERS xasufw_init.h)
collect (PROJECT_LIB_SOURCES xasufw_ipi.c)
collect (PROJECT_LIB_HEADERS xasufw_ipi.h)
collect (PROJECT_LIB_SOURCES xasufw_kat.c)
collect (PROJECT_LIB_HEADERS xasufw_kat.h)
collect (PROJECT_LIB_SOURCES xasufw_sha2handler.c)
collect (PROJECT_LIB_HEADERS xasufw_sha2handler.h)
collect (PROJECT_LIB_SOURCES xasufw_sha3handler.c)
collect (PROJECT_LIB_HEADERS xasufw_sha3handler.h)
collect (PROJECT_LIB_SOURCES xasufw_rsahandler.c)
collect (PROJECT_LIB_HEADERS xasufw_rsahandler.h)
collect (PROJECT_LIB_SOURCES xasufw_ecchandler.c)
collect (PROJECT_LIB_HEADERS xasufw_ecchandler.h)
collect (PROJECT_LIB_SOURCES xasufw_trnghandler.c)
collect (PROJECT_LIB_HEADERS xasufw_trnghandler.h)
collect (PROJECT_LIB_SOURCES xasufw_aeshandler.c)
collect (PROJECT_LIB_HEADERS xasufw_aeshandler.h)
collect (PROJECT_LIB_SOURCES xasufw_main.c)
collect (PROJECT_LIB_HEADERS xasufw_memory.h)
collect (PROJECT_LIB_SOURCES xasufw_modules.c)
collect (PROJECT_LIB_HEADERS xasufw_modules.h)
collect (PROJECT_LIB_SOURCES xasufw_resourcemanager.c)
collect (PROJECT_LIB_HEADERS xasufw_resourcemanager.h)
collect (PROJECT_LIB_SOURCES xasufw_queuescheduler.c)
collect (PROJECT_LIB_HEADERS xasufw_queuescheduler.h)
collect (PROJECT_LIB_SOURCES xasufw_sss.c)
collect (PROJECT_LIB_HEADERS xasufw_sss.h)
collect (PROJECT_LIB_SOURCES xasufw_status.c)
collect (PROJECT_LIB_HEADERS xasufw_status.h)
collect (PROJECT_LIB_SOURCES xasufw_util.c)
collect (PROJECT_LIB_HEADERS xasufw_util.h)
collect (PROJECT_LIB_SOURCES xasufw_intr.c)
collect (PROJECT_LIB_HEADERS xasufw_intr.h)
collect (PROJECT_LIB_SOURCES xasufw_hmachandler.c)
collect (PROJECT_LIB_HEADERS xasufw_hmachandler.h)
collect (PROJECT_LIB_SOURCES xasufw_kdfhandler.c)
collect (PROJECT_LIB_HEADERS xasufw_kdfhandler.h)
collect (PROJECT_LIB_SOURCES xasufw_error_manager.c)
collect (PROJECT_LIB_HEADERS xasufw_error_manager.h)
collect (PROJECT_LIB_SOURCES xasufw_ecieshandler.c)
collect (PROJECT_LIB_HEADERS xasufw_ecieshandler.h)
collect (PROJECT_LIB_SOURCES xasufw_keywraphandler.c)
collect (PROJECT_LIB_HEADERS xasufw_keywraphandler.h)

collect(PROJECT_LIB_DEPS xilstandalone)
collect(PROJECT_LIB_DEPS xil)
collect(PROJECT_LIB_DEPS xiltimer)
collect(PROJECT_LIB_DEPS xilasu)
collect(PROJECT_LIB_DEPS xilasu_mbv)
collect(PROJECT_LIB_DEPS gcc)
collect(PROJECT_LIB_DEPS c)

aux_source_directory(${CMAKE_SOURCE_DIR} _sources)
foreach (source ${_sources})
    get_filename_component(ext ${source} EXT)
    list(APPEND src_ext ${ext})
endforeach()

if (NOT DEFINED PROJECT_TYPE)
find_project_type ("${src_ext}" PROJECT_TYPE)
endif()

if("${PROJECT_TYPE}" STREQUAL "c++")
collect(PROJECT_LIB_DEPS stdc++)
set(CMAKE_C_COMPILER ${CMAKE_CXX_COMPILER})
endif()

collector_list (_deps PROJECT_LIB_DEPS)
list (APPEND _deps ${USER_LINK_LIBRARIES})
message(STATUS "deps is:" ${_deps})

add_subdirectory(aes)
add_subdirectory(ecc)
add_subdirectory(rsa_ecc)
add_subdirectory(sha)
add_subdirectory(task)
add_subdirectory(trng)
add_subdirectory(xfih)
add_subdirectory(hmac)
add_subdirectory(kdf)
add_subdirectory(ecies)
add_subdirectory(keywrap)

if (CMAKE_EXPORT_COMPILE_COMMANDS AND
    (NOT ${YOCTO}))
	set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
	set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
endif()

collector_list (_sources PROJECT_LIB_SOURCES)
collector_list (_headers PROJECT_LIB_HEADERS)

file(COPY ${_headers} DESTINATION ${CMAKE_BINARY_DIR}/include)
string(APPEND CMAKE_C_FLAGS ${USER_COMPILE_OPTIONS})
string(APPEND CMAKE_CXX_FLAGS ${USER_COMPILE_OPTIONS})
string(APPEND CMAKE_C_LINK_FLAGS ${USER_LINK_OPTIONS})
string(APPEND CMAKE_CXX_LINK_FLAGS ${USER_LINK_OPTIONS})
add_dependency_on_bsp(_sources)
add_executable(${APP_NAME}.elf ${_sources})

set_target_properties(${APP_NAME}.elf PROPERTIES LINK_DEPENDS ${CMAKE_SOURCE_DIR}/lscript.ld)
target_link_libraries(${APP_NAME}.elf -Wl,-Map=${APP_NAME}.map -Wl,--build-id=none -Wl,--gc-sections -T\"${CMAKE_SOURCE_DIR}/lscript.ld\" -L\"${CMAKE_LIBRARY_PATH}/\" -L\"${USER_LINK_DIRECTORIES}/\" -Wl,--start-group ${_deps} -Wl,--end-group)
target_compile_definitions(${APP_NAME}.elf PUBLIC ${USER_COMPILE_DEFINITIONS})
target_include_directories(${APP_NAME}.elf PUBLIC ${USER_INCLUDE_DIRECTORIES} ${PROJECT_LIB_HEADERS})
print_elf_size(CMAKE_SIZE ${APP_NAME})
